package com.lanou3g.bookstore.user.admin.web.servlet;

import com.lanou3g.bookstore.user.dao.UserDao;
import com.lanou3g.bookstore.user.domain.AdminUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns = "/adminjsps/login.do")
public class LoginServlet extends HttpServlet {
    private UserDao dao = new UserDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String adminname = request.getParameter("adminname");
        String password = request.getParameter("password");
        AdminUser user = dao.login(adminname, password);
        String msg = "";
        if (user != null) {
            msg = "你好," + user.getUsername();
            request.getSession().setAttribute("adminUser", user);
        } else {
            msg = "请问你是管理员吗?";
        }
        request.setAttribute("msg", msg);
        request.getRequestDispatcher("/adminjsps/msg.jsp").forward(request, response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
